/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pardal.classifiers;

import java.util.ArrayList;
import pardal.core.ArffManipulation;
import pardal.core.Nodo;
import weka.classifiers.Classifier;
import weka.core.Capabilities;
import weka.core.Instance;
import weka.core.Instances;

/**
 * Classe que implementa os classificadores locais nos niveis da hierarquia
 * @author Valdigleis
 */
public class HMLP extends Classifier{

    //Conjunto de classficadores locais.
    private Classifier classifier[];
    //Instancias do problema
    private Instances instance;
    //classe de manipulação
    private ArffManipulation am;
    //opção de classificação obrigatoria nós nos folhas.
    private int opt;
            
    public HMLP(Classifier classifier, Instances instance, int opt) throws Exception {
        this.am = new ArffManipulation(instance);
        am.getHierarchicalClasse();
        this.classifier = Classifier.makeCopies(classifier, am.getmaxlevel());
        this.instance = instance;
        this.opt = opt;
    }
    
    /**
     * Método que realiza o treinamento do HMLP.
     * 
     * @param instances - Instâncias de treinamento.
     * @throws Exception - Execeção caso um ou mais classficiadores locais não possam ser treinados.
     */
    @Override
    public void buildClassifier(Instances instances) throws Exception {
        
        for (int i = 0; i < this.classifier.length; i++) {
            Instances temp = am.buildInstanceLevel(i, instances);
            this.classifier[i].buildClassifier(temp);
        }
        
    }

    @Override
    public double classifyInstance(Instance instance) throws Exception{
        if(this.opt == 1){
            ArrayList<Nodo> leafs = this.am.getLeaf();
            //Vetor que guarda as possiveis previsões
            ArrayList<Double> classificTemp = new ArrayList<>();
            //vetor que guarda o ID do classificador que classificou o nó.
            ArrayList<Integer> idClassfier = new ArrayList<>();
            for(int i = 0; i < this.classifier.length; i++){
                if(this.classifier[i].classifyInstance(instance) != Instance.missingValue()  ){
                    classificTemp.add(this.classifier[i].classifyInstance(instance));
                    idClassfier.add(i);
                }
                
            }
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public double[] distributionForInstance(Instance instance) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Capabilities getCapabilities() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
